package com.example.dao;

import com.example.domain.Product;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ProductDao {

    private Connection connection;

    // 构造函数，接收数据库连接
    public ProductDao(Connection connection) {
        if (connection == null) {
            throw new IllegalArgumentException("数据库连接不能为空");
        }
        this.connection = connection;
    }

    /**
     * Create - 创建产品
     */
    public void createProduct(Product product) throws SQLException {
        if (product == null) {
            throw new IllegalArgumentException("产品信息不能为空");
        }
        String sql = "INSERT INTO tb_product (product_name, price, sale_price, type_id, status, is_top, intro, logo, idx) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, product.getProduct_name());
            stmt.setDouble(2, product.getPrice());
            stmt.setDouble(3, product.getSale_price());
            stmt.setInt(4, product.getType_id());
            stmt.setInt(5, product.getStatus());
            stmt.setInt(6, product.getIs_top());
            stmt.setString(7, product.getIntro());
            stmt.setString(8, product.getLogo());
            stmt.setString(9, product.getIdx());
            stmt.executeUpdate();
        }
    }

    /**
     * Read - 根据ID查询产品
     */
    public Optional<Product> getProductById(int id) throws SQLException {
        if (id <= 0) {
            return Optional.empty();
        }
        String sql = "SELECT * FROM tb_product WHERE id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Product product = new Product();
                    product.setId(rs.getInt("id"));
                    product.setProduct_name(rs.getString("product_name"));
                    product.setPrice(rs.getDouble("price"));
                    product.setSale_price(rs.getDouble("sale_price"));
                    product.setType_id(rs.getInt("type_id"));
                    product.setStatus(rs.getInt("status"));
                    product.setIs_top(rs.getInt("is_top"));
                    product.setIntro(rs.getString("intro"));
                    product.setLogo(rs.getString("logo"));
                    product.setIdx(rs.getString("idx"));
                    return Optional.of(product);
                }
            }
        }
        return Optional.empty();
    }

    /**
     * Read - 查询所有产品
     */
    public List<Product> getAllProducts() throws SQLException {
        List<Product> products = new ArrayList<>();
        String sql = "SELECT * FROM tb_product";
        try (PreparedStatement stmt = connection.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                Product product = new Product();
                product.setId(rs.getInt("id"));
                product.setProduct_name(rs.getString("product_name"));
                product.setPrice(rs.getDouble("price"));
                product.setSale_price(rs.getDouble("sale_price"));
                product.setType_id(rs.getInt("type_id"));
                product.setStatus(rs.getInt("status"));
                product.setIs_top(rs.getInt("is_top"));
                product.setIntro(rs.getString("intro"));
                product.setLogo(rs.getString("logo"));
                product.setIdx(rs.getString("idx"));
                products.add(product);
            }
        }
        return products;
    }

    /**
     * Update - 更新产品信息
     */
    public void updateProduct(Product product) throws SQLException {
        if (product == null || product.getId() <= 0) {
            throw new IllegalArgumentException("无效的产品ID");
        }
        String sql = "UPDATE tb_product SET product_name = ?, price = ?, sale_price = ?, type_id = ?, status = ?, is_top = ?, intro = ?, logo = ?, idx = ? WHERE id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setString(1, product.getProduct_name());
            stmt.setDouble(2, product.getPrice());
            stmt.setDouble(3, product.getSale_price());
            stmt.setInt(4, product.getType_id());
            stmt.setInt(5, product.getStatus());
            stmt.setInt(6, product.getIs_top());
            stmt.setString(7, product.getIntro());
            stmt.setString(8, product.getLogo());
            stmt.setString(9, product.getIdx());
            stmt.setInt(10, product.getId());
            stmt.executeUpdate();
        }
    }

    /**
     * Delete - 删除产品
     */
    public void deleteProduct(int id) throws SQLException {
        if (id <= 0) {
            throw new IllegalArgumentException("无效的产品ID");
        }
        String sql = "DELETE FROM tb_product WHERE id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }
}